package cn.burnit.view;

import cn.burnit.pojo.Book;
import cn.burnit.utils.ConnectUtils;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Vector;

/**
 * 指定条件查询图书界面（模糊查询）
 */
public class CheckBookForm extends JFrame {
    private JLabel bookName, author, publisher, publishTime;
    private JTextField bookNameField, authorField, publisherField, publishTimeField;
    private JButton btn_Cancel, btn_Check;
    private JScrollPane scrollPane;
    private JTable table= new JTable();

    public CheckBookForm() {

        Container container = getContentPane();
        container.setLayout(null);
        btn_Check = new JButton("查询");
        btn_Check.setBounds(300, 80, 80, 20);
        btn_Cancel = new JButton("关闭");
        btn_Cancel.setBounds(400, 80, 80, 20);
        btn_Cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        bookName = new JLabel("图书名称");
        bookName.setFont(new Font("宋体", Font.PLAIN, 16));
        bookName.setBounds(170, 20, 100, 20);
        author = new JLabel("作者");
        author.setFont(new Font("宋体", Font.PLAIN, 16));
        author.setBounds(430, 20, 100, 20);
        publisher = new JLabel("出版社");
        publisher.setFont(new Font("宋体", Font.PLAIN, 16));
        publisher.setBounds(170, 45, 100, 20);
        publishTime = new JLabel("出版时间");
        publishTime.setFont(new Font("宋体", Font.PLAIN, 16));
        publishTime.setBounds(430, 45, 100, 20);
        bookNameField = new JTextField();
        bookNameField.setBounds(240, 20, 160, 20);
        authorField = new JTextField();
        authorField.setBounds(500, 20, 160, 20);
        publisherField = new JTextField();
        publisherField.setBounds(240, 45, 160, 20);
        publishTimeField = new JTextField();
        publishTimeField.setBounds(500, 45, 160, 20);

        //table=new JTable();
        table.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{"图书编号", "图书名称", "图书类别", "作者", "译者", "出版社", "出版日期", "定价", "库存数量","已借数量"}
            )
        );
        table.getColumnModel().getColumn(0).setPreferredWidth(53);
        table.getColumnModel().getColumn(1).setPreferredWidth(53);
        table.getColumnModel().getColumn(2).setPreferredWidth(53);
        table.getColumnModel().getColumn(3).setPreferredWidth(53);
        table.getColumnModel().getColumn(4).setPreferredWidth(53);
        table.getColumnModel().getColumn(5).setPreferredWidth(53);
        table.getColumnModel().getColumn(6).setPreferredWidth(53);
        table.getColumnModel().getColumn(7).setPreferredWidth(53);
        table.getColumnModel().getColumn(8).setPreferredWidth(53);
        table.getColumnModel().getColumn(9).setPreferredWidth(53);

        scrollPane = new JScrollPane();
        scrollPane.setViewportView(table);
        btn_Check.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                checkByBooknameOrPublisher(e);
            }
        });

        scrollPane.setBounds(20, 120, 750, 400);

        container.add(bookName);
        container.add(bookNameField);
        container.add(author);
        container.add(authorField);
        container.add(publisher);
        container.add(publisherField);
        container.add(publishTime);
        container.add(publishTimeField);
        container.add(btn_Check);
        container.add(btn_Cancel);
        container.add(scrollPane);

        setTitle("图书查询");
        setSize(800, 600);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        //getAllBook(new Book());
        //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }




    private void checkByBooknameOrPublisher(ActionEvent e) {

        String bookame = bookNameField.getText().trim();
        String publisher = publisherField.getText().trim();
        Book book = new Book();
        book.setBookname(bookame);
        book.setPublisher(publisher);
        //调用table填充函数，根据查询结果重新填充表格
        fillBookTable(bookame,publisher);
    }

    private void fillBookTable(String bookname,String publisher) {

        DefaultTableModel dtm = (DefaultTableModel) table.getModel();
        //填充表格时设置成0行（相当于归零处理）
        dtm.setRowCount(0);

        ConnectUtils.getDriver();
        Connection connection=null;
        try {
            connection=ConnectUtils.getConnection();


            String listBookSql="select * from book where bookname like ? and publisher like ?";
            PreparedStatement ps = connection.prepareStatement(listBookSql);

            ps.setString(1,"%"+bookname+"%");
            ps.setString(2,"%"+publisher+"%");
            ResultSet result = ps.executeQuery();
            while (result.next()){
                String id = result.getString(1);
                String bookname1 = result.getString(2);
                String booktype = result.getString(3);
                String author = result.getString(4);
                String translator = result.getString(5);
                String publisher1 = result.getString(6);
                Date publish_time = result.getDate(7);
                float price = result.getFloat(8);
                int stock = result.getInt(9);
                int outloan=result.getInt(10);

                Vector v = new Vector<>();
                v.add(id);
                v.add(bookname1);
                v.add(booktype);
                v.add(author);
                v.add(translator);
                v.add(publisher1);
                v.add(publish_time);
                v.add(price);
                v.add(stock);
                v.add(outloan);
                dtm.addRow(v);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new CheckBookForm();
    }
}